package com.cgw.room.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

/**
 * 这个类用于存储页面快照。以列表页面为例，存储了第一次刷新的数据列表、列表刷新时间、是否还有更多数据
 * Created by huangyifei on 16/12/26.
 */

public class SnapshotCache {
    private static final int LIST_CACHE_VERSION = 2;
    private static final int LIST_CACHE_COUNT = 3;

//    public static DiskLruCache getListCache() {
//        File cacheFile = new File(MainApp.getInstance().getCacheDir(), "list");
//        DiskLruCache cache = null;
//        try {
//            cache = DiskLruCache.open(cacheFile, LIST_CACHE_VERSION, LIST_CACHE_COUNT, 20000000);//20M 文件缓存
//        } catch (IOException e) {
//        }
//        return cache;
//    }

    public static void writeObject(File file, Object data) throws IOException {
        OutputStream out = new FileOutputStream(file);
        ObjectOutputStream objout = new ObjectOutputStream(out);
        objout.writeObject(data);
        objout.close();
        out.close();
    }

    public static Object readObject(File file) throws IOException, ClassNotFoundException {
        InputStream in = new FileInputStream(file);
        ObjectInputStream objin = new ObjectInputStream(in);
        return objin.readObject();
    }


    private static final int PAGE_CACHE_VERSION = 1;
    private static final int PAGE_CACHE_COUNT = 1;

//    public static DiskLruCache getPageCache() {
//        File cacheFile = new File(MainApp.getInstance().getCacheDir(), "page");
//        DiskLruCache cache = null;
//        try {
//            cache = DiskLruCache.open(cacheFile, PAGE_CACHE_VERSION, PAGE_CACHE_COUNT, 20000000);//20M 文件缓存
//        } catch (IOException e) {
//        }
//        return cache;
//    }

}
